using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Betarium.PotonEditor
{
	public class ToolInfoPaste : ToolInfo
	{
		protected bool m_bMove;
		private Image m_PasteImage;

		public Image PasteImage
		{
			get { return m_PasteImage; }
			set { m_PasteImage = value; }
		}
	
		
		public ToolInfoPaste(EditViewForm form)
			: base(form)
		{
		}

		public void Commit()
		{
			if (m_PasteImage != null)
			{
                this.m_Form.AddUndo();
                Graphics graph = Graphics.FromImage(m_Form.Image);
				graph.DrawImage(m_PasteImage, m_Form.SelectState.SelectRect, new Rectangle(Point.Empty, m_PasteImage.Size), GraphicsUnit.Pixel);
				graph.Dispose();
				m_Form.SetModified(true);
			}
			Reset();
//			m_Form.SetPainter(EditViewForm.ToolType.Select);
		}
		public override void OnMouseDown(object sender, MouseEventArgs e)
		{
			m_Form.SelectState.OnMouseDown(sender, e);
			Point cursorPos = m_Form.TransformClientToImage(e.Location);
			Rectangle selectRect = m_Form.SelectState.SelectRect;

            if (e.Button == MouseButtons.Left && e.Clicks == 2 && m_PasteImage != null)
            {
                Commit();
                return;
            }

            if (e.Button == MouseButtons.Left && m_PasteImage != null)
			{
				if (selectRect.Contains(cursorPos))
				{
					m_bMove = true;
				}
				else
				{
					m_bMove = false;
					Reset();
				}
			}
		}
		public override void OnMouseUp(object sender, MouseEventArgs e)
		{
			bool bMouseLeftDown = m_Form.SelectState.MouseLeftDown;
			Point mouseMoveBefore = m_Form.SelectState.MouseBeforePos;
			m_Form.SelectState.OnMouseUp(sender, e);
			/*
			if (m_bMove)
			{
				Graphics graph = Graphics.FromImage(m_Form.Image);
				graph.DrawImage(m_PasteImage, m_Form.Selection.SelectRect, new Rectangle(Point.Empty, m_PasteImage.Size), GraphicsUnit.Pixel);
				graph.Dispose();
				m_Form.SelectClear();
				m_bMove = false;
				m_Form.SetPainter(ViewForm.ToolType.Select);
			}
			*/
		}
		public override void OnMouseMove(object sender, MouseEventArgs e)
		{
			m_Form.SelectState.OnMouseMove(sender, e);
			Point cursorPos = m_Form.SelectState.MouseCurrentPos;
			Point mouseMoveBefore = m_Form.SelectState.MouseBeforePos;
			Rectangle selectRect = m_Form.SelectState.SelectRect;
			if (m_Form.SelectState.MouseLeftDown && m_bMove)	
			{
				Point posMove = cursorPos;
				posMove.Offset(-mouseMoveBefore.X, -mouseMoveBefore.Y);
                if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                {
                    Point posTemp = cursorPos;
                    posTemp.Offset(-m_Form.SelectState.MouseBeginPos.X, -m_Form.SelectState.MouseBeginPos.Y);
                    if (Math.Abs(posTemp.X) > Math.Abs(posTemp.Y))
                    {
                        posMove.Y = 0;
                    }
                    else if (Math.Abs(posTemp.X) < Math.Abs(posTemp.Y))
                    {
                        posMove.X = 0;
                    }
                }
                selectRect.Offset(posMove);
				m_Form.SelectState.SetRect(selectRect);
			}
			if (selectRect.Contains(cursorPos))
			{
				m_Form.Cursor = Cursors.SizeAll;
			}
			else
			{
				m_Form.Cursor = Cursors.Arrow;
			}
		}
		public override void OnPaint(object sender, PaintEventArgs e)
		{
			if (m_PasteImage != null)
			{
				e.Graphics.DrawImage(m_PasteImage, m_Form.SelectState.SelectRect.Left, m_Form.SelectState.SelectRect.Top, new Rectangle(new Point(), m_PasteImage.Size), GraphicsUnit.Pixel);
				Color color = Color.FromArgb(128, Color.Gray);
				e.Graphics.FillRectangle(new SolidBrush(color), m_Form.SelectState.SelectRect);
			}
		}

		public void Reset()
		{
			if (m_PasteImage != null)
			{
				m_PasteImage.Dispose();
				m_PasteImage = null;
			}
			m_Form.SetPainter(EditViewForm.ToolType.Select);
		}
	}
}
